from JCI_Config import get_col_data, auto_run_seconds, auto_run_hours
from JCI_Model import *
from JCI_GetData import *
from datetime import datetime

column_table_name = 'column'
contents_table_name = 'contents'


def save_file(data):
    with open("jci_news_latest.json", "w", encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False)


def get_news_latest_data(cid=0):
    re_data = mydb.select(contents_table_name,
                          fields=['news_id', 'title', 'cid as column_id', 'column_name', 'absolute_url', 'thumb',
                                  'timestamp', 'is_home'],
                          cond_dict={'cid': cid},
                          order=' order by timestamp desc ,news_id desc ',
                          limit='limit 0,15')
    news_list = []
    for item in re_data:
        news_list.append({
            'news_id': item[0], 'title': item[1],
            'column_id': item[2], 'column_name': item[3],
            'absolute_url': item[4], 'thumb': item[5],
            'timestamp': item[6], 'is_home': item[7],
        })
    count = mydb.select(contents_table_name, fields=['count(news_id) as total'], cond_dict={'cid': cid})
    news_column_list = {
        'column_id': cid,
        'list': news_list,
        'total': count[0][0]
    }
    # save_json_file(news_column_list)
    # print(news_column_list)
    return news_column_list


def update_list_contents():
    # column_data_handler()
    # re_data = mydb.select(column_table_name, fields=['column_id', 'column_name', 'column_url'])
    start_now = int(time.time())
    start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_now))
    next_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_now + auto_run_seconds))
    print('更新任务开始，%s' % start_time)
    re_data = get_col_data()
    total = 0
    news_latest_list = []
    for item in re_data:
        insert_count = 0
        news_list = get_column_list_data(item['cid'], item['columnName'], item['columnUrl'])
        for list_item in news_list:
            chk_re = mydb.select(contents_table_name, {'news_id': list_item['news_id']})
            if len(chk_re) == 0:
                news_contents = get_contents_data(list_item['absolute_url'])
                list_item['thumb'] = news_contents[0]
                list_item['news_content'] = news_contents[1]
                list_item['news_attaches'] = news_contents[2]
                re_ist = mydb.insert(contents_table_name, list_item)
                if re_ist > 0:
                    insert_count += 1
        item_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print('【%s】栏目新增【%d】条新闻，%s' % (item['columnName'], insert_count, item_time))
        total += insert_count
        news_latest_list.append(get_news_latest_data(item['cid']))
    end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print('一共新增【%d】条新闻，%s，下一次运行时间：%s' % (total, end_time, next_time))
    save_file(news_latest_list)


def update_thumb():
    cond = {'is_home': 0}
    order_by = ''
    fields = ['news_id', 'news_content']
    contents_db_data = mydb.select(contents_table_name, cond, order_by, fields)
    print('当前需要更新【%d】条数据，请耐心等待……' % len(contents_db_data))
    update_count = 0
    for content_item in contents_db_data:
        html_doc = PythonQuery(content_item[1])
        tmp_src = html_doc('img').attr.src
        news_thumb = tmp_src if tmp_src is not None else ''
        where_cond = {'news_id': content_item[0]}
        update_cond = {'thumb': news_thumb}
        mydb.update(contents_table_name, update_cond, where_cond)
        update_count += 1
        print('已更新【%d】条数据，当前新闻id是【%d】' % (update_count, content_item[0]))
    print('更新【%d】条数据完成' % update_count)


def contents_data(news_col=0, news_id=0):
    cond = {'is_home': 0}
    if news_col != 0 and news_id != 0:
        cond['news_col'] = news_col
        cond['news_id'] = news_id
    order_by = ''
    fields = ['absolute_url', 'news_id', 'news_col']
    contents_db_data = mydb.select(contents_table_name, cond, order_by, fields)
    print('当前需要采集【%d】条数据，请耐心等待……' % len(contents_db_data))
    update_count = 0
    for content_item in contents_db_data:
        news_contents = get_contents_data(content_item[0])
        where_cond = {'news_id': content_item[1], 'news_col': content_item[2]}
        update_cond = {'thumb': news_contents[0], 'news_content': news_contents[1],
                       'news_attaches': news_contents[2]}
        mydb.update(contents_table_name, update_cond, where_cond)
        update_count += 1
        print('已采集【%d】条数据，当前新闻id是【%d】' % (update_count, content_item[1]))
    print('采集【%d】条数据完成' % update_count)


def contents_list_data(column_id=''):
    cond = ''
    if column_id != '':
        cond = {'column_id': column_id}
    else:
        print('请指定采集栏目id')
        exit()
    column_db_data = mydb.select(column_table_name, cond)
    column_list = []
    insert_keys = ['absolute_url', 'column_name', 'news_col', 'news_id', 'title', 'cid', 'timestamp', 'is_home']
    column_name = column_db_data[0][2]
    news_pages = column_db_data[0][6]
    news_nums = column_db_data[0][5]
    print('当前采集栏目【%s】，需要采集【%d】个页面，共【%d】条数据，请耐心等待……' % (column_name, news_pages, news_nums))
    for column_item in column_db_data:
        # print(column_item)
        tmp_list = get_contents_list_data(column_item)
        column_list += tmp_list
        mydb.insertMany(contents_table_name, insert_keys, tmp_list)
    print('【%s】栏目一共采集【%d】条新闻数据' % (column_name, len(column_list)))


def column_data_handler():
    update_count = 0
    insert_count = 0
    column_list_cfg_data = get_column_data()
    where_key = ['domain', 'column_name', 'column_url', 'column_id']
    update_key = ['news_pages', 'news_nums']
    for column_cfg in column_list_cfg_data:
        where_cond = dict([(key, column_cfg[key]) for key in where_key])
        update_cond = dict([(key, column_cfg[key]) for key in update_key])
        chk_re = mydb.select(column_table_name, where_cond)
        if len(chk_re) > 0:
            re_up = mydb.update(column_table_name, update_cond, where_cond)
            update_count += re_up
        else:
            re_ist = mydb.insert(column_table_name, column_cfg)
            if re_ist > 0:
                insert_count += 1
    print('更新 %d 条数据，新增 %d 条数据' % (update_count, insert_count))
